#ifndef QRUNNINGMANAGER_H
#define QRUNNINGMANAGER_H

#include "runninglibglobal.h"
#include "../device/qbasedefine.h"

#include <QObject>
#include <QMainWindow>
#include <QScriptEngine>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QTimer>

class QAbstractHost;
class QProjectCore;
class QBaseDialogWidget;
class QServerManager;
struct tagDeviceInfo;
class QSettingManager;
class QDataBaseManager;

#include <QThread>

class QWorkThread: public QThread
{
    Q_OBJECT
public:
    void    run()
    {
        emit work();
    }

signals:
    void    work();
};

class RUNNINGLIB_EXPORT QRunningManager : public QObject
{
    Q_OBJECT
public:
    explicit QRunningManager(const QString &status,QObject *parent = 0);
    ~QRunningManager();
    
    void    load(const QString &path);

    void    release();

    void    start();
    void    stop();

    void    init_script_engine();

protected:
    void    connect_exec();
    void    focus_changed(QWidget* now);
    void    exec(const QString &code,const QMap<QString,QVariant> &param,QMap<QString,QVariant> & ret);
protected slots:
    void    load_timer();
    void    timer();
    void    do_exec(const QString& code,const QMap<QString,QVariant> &param,QMap<QString,QVariant> &ret);

    void    changed_data(const QString &uuid,const QVariant &value);
    void    changed_property(const QString &name,const QVariant &value);
    void    data_changed(const QString& uuid);
protected:
    QScriptValue get_script_value(const QVariant& value);
    QScriptValue get_host_value(QAbstractHost* host);
    bool    eventFilter(QObject *, QEvent *);
signals:
    void    start_signal();
    void    stop_signal();
    void    load_signal(bool success);
public slots:
    void    show_widget(QWidget* widget);
    void    show_dialog(QAbstractHost* host);

    void    update_success();

    void    data_save_timer();
protected:
    void    show_keyboard(QAbstractHost* host);
protected:
    QProjectCore        *m_project;
    QMainWindow         *m_main_window;
    QBaseDialogWidget   *m_dialog_base_widget;
    QWidget*             m_last_widget;
    QServerManager      *m_server_manager;
    tagDeviceInfo       m_device_info;
    QSettingManager     *m_setting_manager;
    QString             m_status;
    QScriptEngine       m_script_engine;
    QScriptValue        m_global_value;
    QGraphicsScene      *m_scene;
    QGraphicsView       *m_view;
    QMap<QWidget*,QGraphicsProxyWidget*>    m_widget_to_proxy;
    QTimer              *m_timer;
    QWidget*            m_last_focus_widget;
    QAbstractHost       *m_keyboard_host;
    QString             m_project_path;
    QWorkThread         *m_workthread;
    QDataBaseManager    *m_database_manager;
    QTimer              *m_data_save_timer;
    QMap<QString,QDateTime> m_last_save_time;
};

#endif // QRUNNINGMANAGER_H
